var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var routerINDEX = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// 改写
var http = require('http');
var server = http.createServer(app);


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));// 设置静态文件目录


app.use('/', routerINDEX);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

// 设置跨域
app.all("*", (req, res, next) => {
	res.header("Access-Control-Allow-Origin", "*");              	// 允许任意外源访问
	res.header("Access-Control-Allow-Headers", "Content-Type");		// 自定义请求首部字段
	res.header("Access-Control-Allow-Methods", "*");    		 	// 允许所有请求方法
	res.header("Content-Type", "application/json;charset=utf-8");	// 设置数据返回类型为json，字符集为utf8
	if(req.method.toLowerCase() === 'options'){
		res.send(200); // 让 options 尝试快速结束请求
	} else {
    	next();
    }
})

// 测试 - 时间功能
// var timeExchange = require('./tools/timeExchange.js');
// timeExchange.timeStamp_TimeString(Date.now())  // 1、时间戳-》时间字符串
// timeExchange.timeStamp_TimeDate()    // 2、时间戳-》时间Date格式
// timeExchange.timeDate_TimeString()   // 3、时间Date格式-》时间字符串
// timeExchange.timeDate_TimeStamp()    // 4、时间Date格式-》时间戳
// timeExchange.timeString_TimeDate('2022-06-15')   // 5、时间字符串-》时间Date格式
// timeExchange.timeString_TimeStamp('2022-06-15')  // 6、时间字符串-》时间戳

// var timeExchange = require('./tools/timeExchange.js');
// // 获取当前时间的时间戳
// var now = new Date();
// console.log(`当前时间戳是----${now.getTime()}`)
// console.log(`当前时间是----${timeExchange.timeStamp_TimeString(now.getTime())}`)
// // 在当前时间上加上3天的毫秒数
// now.setTime(now.getTime() + (3 * 24 * 60 * 60 * 1000)); // 24小时/天，60分钟/小时，60秒/分钟，1000毫秒/秒
 
// // 获取3天后的时间戳
// var threeDaysLaterTimestamp = now.getTime();
// console.log(`三天后时间戳是----${threeDaysLaterTimestamp}`)
// const timeString = timeExchange.timeStamp_TimeString(threeDaysLaterTimestamp)
// console.log(`三天后时间是----${timeString}`)

// var timeExchange = require('./tools/timeExchange.js');
//   var times = timeExchange.timeString_3daysLater()
//   console.log(`当前时间是: ${times.current}   三天后时间:${times.expire}` )


// 引入函数
// const { sortDefault } = require('./routes/menu/jss_math');

// const data_after = sortDefault(data, 'children', 'type')
// console.log('排序后 ', JSON.stringify(data_after))

server.listen('3004', () => {
  console.log("服务启动成功！！")
  // console.log("请求访问 " + new Date())
})
